Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

911
Visualizações
Laravel - Mongodb [ jenssegers/laravel-mongodb ] - Schema Builder

I have an web app with mongodb, using jenssegers package.

Since I'm using mongodb, do I need to create any migration table? Jenssegers database driver also has (limited) schema builder support

Schema::create('users', function($collection)
{
    $collection->index('name');

    $collection->unique('email');
});

I found two different "answers". This guy is using the schema builder

enter image description here

And the Mulkave answer:

Q: "When i run the comman "php artisan migrate" then i am getting following error where as migration table is created into mongodb database:"

A: "You might have misinterpreted the purpose of MongoDB being a document database, you do not need to have migrations in document-based databases and this is what they're good a. They have dynamic schemas so all you'll have to do is save your model regardless of what attributes they have which means you'll have to tighten your application's business logic to make sure the models are being saved as you expected."


So, do I need migrations table? Thanks!

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I know this is a bit late, but I thought I'd throw in a bit of code here. I like to have an indexed model for two reasons:

  1. I can use it like an interface, ie I am forced to enter this information in because it's part of what the model requires. This ensures that I have a clean dataset. This is just preference, it's not a requirement.
  2. Indexed searches are faster than when they aren't index. I haven't done enough research into this to figure out if this is the case with jenssegers library. But it makes sense to me that, if you use a schema to set up a collection and set it to index these fields, then it would be faster when searching for records within this collection. You can also set this up manually from the Mongo side but I like that you can do it with this Eloquent extension.

So to the person reading this, if I've convinced you with this unverified argument and you'd like to set up a schema, I've found that it's easy to set up the collection but not easy to drop it. If you want to do a full migration (be aware that you'll lose all of your data when you do this) then you can use the code below:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateYourCollectionTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::connection('mongodb')->create('collection_name', function ($collection) {
            $collection->index('field_1');
            $collection->index('field_2');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        DB::connection('mongodb')->drop(['collection_name']);
    }
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda